package edu.uic.kott.activities;

import java.util.List;

import edu.uic.keyoutcomestrackertool.R;
import edu.uic.kott.db.KottDataSource;
import edu.uic.kott.model.Course;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
//import android.os.AsyncTask;

public class Courses extends ListActivity {

	private static final String LOGTAG = "KOTT";

	KottDataSource datasource; 

	List<Course> allCourses;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_courses);

		datasource = new KottDataSource(this);

		Bundle extras = getIntent().getExtras();

		if (extras != null){
			if("update".equalsIgnoreCase(extras.getString("option"))){
				Course course = new Course();

				course.setCourseId(Long.parseLong(extras.getString("className")));
				course.setCourseName(extras.getString("className"));
				course.setStartDate(extras.getString("startDate"));
				course.setEndDate(extras.getString("endDate"));
				course.setClassDays(extras.getString("days"));
				course.setStartTime(extras.getString("startTime"));
				course.setEndTime(extras.getString("endTime"));
				course.setTargetedWSS(extras.getString("tWSS"));
				course.setClassLocation(extras.getString("classLocation"));
				course.setInstructor(extras.getString("instructorId"));
				course.setTa(extras.getString("taId"));

				datasource.updateCourse(course);
			}
		}
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);

		Course course = allCourses.get(position);		

		Intent intent = new Intent(this, CourseDetails.class);
		intent.putExtra("courseId", String.valueOf(course.getCourseId()));
		startActivity(intent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_add, menu);		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent = new Intent(this, AddEditCourse.class);
		startActivity(intent);

		return super.onOptionsItemSelected(item);
	}

	public void refreshDisplay(){		

		//new FindAllCourses().execute((Object[])null);
		allCourses = datasource.findAllCourses("Courses");

		if(allCourses.size() > 0){	
			ArrayAdapter<Course> allCoursesAdapter = new ArrayAdapter<Course>(this, android.R.layout.simple_list_item_1, allCourses);
			setListAdapter(allCoursesAdapter);
		}
		else{
			Log.i(LOGTAG, "No courses added!");
		}
	}

	/*class FindAllCourses extends AsyncTask<Object, Object, List>{
		@Override
		protected List doInBackground(Object... params) {
			return datasource.findAllCourses();
		}

		protected void onPostExecute(List result){
			allCourses = result;

			if(allCourses.size() > 0){	
				ArrayAdapter<Course> allCoursesAdapter = new ArrayAdapter<Course>(Courses.this, android.R.layout.simple_list_item_1, allCourses);
				setListAdapter(allCoursesAdapter);
			}
			else{
				Log.i(LOGTAG, "No courses added!");
			}			
		}			
	}*/

	@Override
	protected void onResume() {
		refreshDisplay();
		super.onResume();
	}
}
